package com.site.user.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.site.data.JPABaseDAOImpl;
import com.site.user.pojo.TbPrivilege;
import com.site.user.pojo.TbPrivilegePK;

/**
 * @author      zhanjun
 * @date        2010-12-8
 * @version     1.0
 * @description 用户表服务层
 * 
 */
@Service("tbPrivilegeService")
public class TbPrivilegeService extends JPABaseDAOImpl<TbPrivilege> implements ITbPrivilegeService {

	
	/* (non-Javadoc)
	 * @see com.site.user.service.ITbPrivilegeService#getOptnById(java.lang.String, java.lang.String)
	 */
	@Override
	public List getOptnById(Object[] idroles, String idresc) {

		
		if ((idroles==null) || (StringUtils.isNotEmpty(idresc))) {
			return null;
		}
		String idrole=StringUtils.join(idroles, "','");
		String sql = "";
		if ("99".equals(idrole)) {
			sql = "select '99','99','99',ID_OPERATION,OPET_NAME from tb_operation";
		} else {
			sql = "select DISTINCT y.ID_ROLE,y.ID_SYSTEM,y.ID_RESOURCE,y.ID_OPERATION,y.OPET_NAME from v_privige y where y.id_role in ('"
					+ idrole + "') and y.id_resource='" + idresc + "'";
		}
		List<Object[]> rs = null;
		if (StringUtils.isNotEmpty(sql)) {
			rs = jdbcExecQuery(sql, null);
		}
		if (rs != null) {
			List<Map<String, String>> oList = new ArrayList<Map<String, String>>();
			Map<String, String> optmap = null;
			for (int k = 0; k < rs.size(); k++) {
				Object[] entity = (Object[]) rs.get(k);

				String id_role = (String) entity[0];
				String id_system = (String) entity[1];
				String id_resource = (String) entity[2];
				String id_operation = (String) entity[3];
				String opet_name = (String) entity[4];
				String id = id_role + "-" + id_system + "-" + id_resource + "-" + id_operation;
				optmap = new HashMap<String, String>();
				optmap.put("privilegeId", id);
				optmap.put("opetName", opet_name);

				oList.add(optmap);
			}
			return oList;
		}
		return null;

	}

	public String deletePrivilege(String ids) {
		if (StringUtils.isNotEmpty(ids)){
			return null;
		}
		String[] id = ids.split("-");
		String delsql = "delete FROM TB_PRIVILEGE where ID_ROLE='" + id[0] + "' and ID_SYSTEM='" + id[1]
				+ "' and ID_RESOURCE='" + id[2] + "' and ID_OPERATOR='" + id[3] + "'";
		int i = jdbcExecUpdate(delsql);

		return i + "条数据删除成功!";
	}

	public String savePrivilege(String idrole) {//保存数据到正式表中
		if (StringUtils.isNotEmpty(idrole)){
			return null;
		}
		List<TbPrivilege> entitylist = new ArrayList<TbPrivilege>();

		String select = "select ID_RESOURCE,ID_OPERATOR,ID_ROLE,ID_SYSTEM from tb_privilege_temp where ID_ROLE='"
				+ idrole + "'";
		List<Object[]> rs = jdbcExecQuery(select, null);

		for (int i = 0; i < rs.size(); i++) {
			Object[] entity = (Object[]) rs.get(i);
			String id_resource = (String) entity[0];
			String id_operator = (String) entity[1];
			String id_role = (String) entity[2];
			String id_system = (String) entity[3];

			String testsql = "idRole='" + id_role + "' and idSystem='" + id_system + "' and idResource='" + id_resource
					+ "' and idOperator='" + id_operator + "'";
			Long count = queryCount(testsql, null);
			// 先检查是否已经有重复的数据

			if (count == 0) {// 没有重复的数据的插入
				TbPrivilege entityt = new TbPrivilege();
				TbPrivilegePK pk = new TbPrivilegePK();
				pk.setIdOperator(id_operator);
				pk.setIdResource(id_resource);
				pk.setIdRole(idrole);
				entityt.setTbPrivilegePK(pk);
				entityt.setIdSystem(id_system);
				entitylist.add(entityt);
			}

		}

		if (entitylist.size() > 0) {
			jdbcSaveEntityBatch(entitylist);
		}
		String delete = "delete FROM tb_privilege_temp where id_role='" + idrole + "'";
		jdbcExecUpdate(delete);//删除临时表中的数据
		return entitylist.size() + "条权限数据保存成功！";
	}

	@Override
	public String loadPrivGrid(String idrole, String type) {
		
		StringBuilder xmlString = new StringBuilder();
		xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		xmlString.append("<rows>\n");

		if (StringUtils.isNotEmpty(idrole) && StringUtils.isNotEmpty(type)) {

			String vtable = "";
			if (type.equals("temp")) {//临时表
				vtable = "V_PRIVIGE_TEMP";
			} else {
				//正式表
				vtable = "V_PRIVIGE";
			}
			String sql = "select y.ID_ROLE,y.ID_RESOURCE,y.RES_NAME,y.ID_OPERATION,y.OPET_NAME,y.ID_SYSTEM,y.ROLE_NAME from "
					+ vtable + " y where y.id_role='" + idrole + "'";

			List rs = jdbcExecQuery(sql, null);
			if (rs != null) {

				for (int k = 0; k < rs.size(); k++) {
					Object[] entity = (Object[]) rs.get(k);
					String idro = (String) entity[0];
					String idrs = (String) entity[1];
					String resname = (String) entity[2];
					String idper = (String) entity[3];
					String operat = (String) entity[4];
					String idsys = (String) entity[5];
					String rolename = (String) entity[6];

					String id = idro + "-" + idsys + "-" + idrs + "-" + idper;
					xmlString.append("<row id=\"" + id + "\">\n");
					xmlString.append("<cell>" + rolename + "</cell>\n");
					xmlString.append("<cell>" + resname + "</cell>\n");
					xmlString.append("<cell>" + operat + "</cell>\n");
					xmlString.append("</row>\n");

				}

			}

		}

		xmlString.append("</rows>\n");
		return xmlString.toString();
	}

}
